package com.zy.fee.roadpark.dao;

import android.content.Context;
import com.yanzhenjie.nohttp.FileBinary;
import com.yanzhenjie.nohttp.RequestMethod;
import com.yanzhenjie.nohttp.rest.Response;
import com.yanzhenjie.nohttp.rest.SimpleResponseListener;
import com.yanzhenjie.nohttp.rest.StringRequest;
import com.zy.fee.roadpark.config.Urls;
import com.zy.fee.roadpark.model.apimodel.APIM_LotList;
import com.zy.fee.roadpark.model.apimodel.APIM_NodeList;
import com.zygk.library.model.CommonResult;
import com.zygk.library.util.CallServer;
import com.zygk.library.util.HttpRequest;
import com.zygk.library.util.JsonUtil;
import com.zygk.library.util.ToastUtil;
import java.io.File;

/**
 * Created by Wikison on 2018/9/13.
 */
public class NpNodeLogic {
  //获取管理的停车场列表
  public static void lot_list(Context mContext, String adminID,
      final HttpRequest.HttpCallback callback) {
    StringRequest request = new StringRequest(Urls.NPNODE_LOT_LIST, RequestMethod.POST);
    request.add("adminID", adminID);
    SimpleResponseListener<String> listener = new SimpleResponseListener<String>() {
      @Override public void onStart(int what) {
        super.onStart(what);
        callback.onStart();
      }

      @Override public void onSucceed(int what, Response<String> response) {
        super.onSucceed(what, response);
        APIM_LotList result = JsonUtil.jsonToObject(response.get().toString(), APIM_LotList.class);
        if (result == null) return;
        if (result.getStatus() == 1) {
          callback.onSucceed(result);
        } else {
          ToastUtil.showMessage(mContext, result.getInfo());
        }
      }

      @Override public void onFailed(int what, Response<String> response) {
        super.onFailed(what, response);
        ToastUtil.showNetErrorMessage(mContext);
      }

      @Override public void onFinish(int what) {
        super.onFinish(what);
        callback.onFinish();
      }
    };
    CallServer.getInstance().request(0, request, listener);
  }

  //获取停车场停车位列表
  public static void node_list(Context mContext, String lotID, int page, int rows,
      final HttpRequest.HttpCallback callback) {
    StringRequest request = new StringRequest(Urls.NPNODE_NODE_LIST, RequestMethod.POST);
    //request.add("adminID", PreferencesHelper.userManager().getUserID());
    request.add("lotID", lotID);
    request.add("page", page);
    request.add("rows", rows);
    SimpleResponseListener<String> listener = new SimpleResponseListener<String>() {
      @Override public void onStart(int what) {
        super.onStart(what);
        callback.onStart();
      }

      @Override public void onSucceed(int what, Response<String> response) {
        super.onSucceed(what, response);
        APIM_NodeList result = JsonUtil.jsonToObject(response.get(), APIM_NodeList.class);
        if (result == null) return;
        if (result.getStatus() == 1) {
          callback.onSucceed(result);
        } else {
          ToastUtil.showMessage(mContext, result.getInfo());
        }
      }

      @Override public void onFailed(int what, Response<String> response) {
        super.onFailed(what, response);
        ToastUtil.showNetErrorMessage(mContext);
      }

      @Override public void onFinish(int what) {
        super.onFinish(what);
        callback.onFinish();
      }
    };
    CallServer.getInstance().request(0, request, listener);
  }

  //创建无地磁收费临停单
  public static void create_node_Record(Context mContext, String adminID, String lotID,
      String carNumber, String startTime, double payMoney, String code,
      final HttpRequest.HttpCallback callback) {
    StringRequest request = new StringRequest(Urls.NPNODE_CREATE_NODE_RECORD, RequestMethod.POST);
    request.add("adminID", adminID);
    request.add("lotID", lotID);
    request.add("startTime", startTime);
    request.add("carNumber", carNumber);
    request.add("payMoney", payMoney);
    request.add("code", code);
    SimpleResponseListener<String> listener = new SimpleResponseListener<String>() {
      @Override public void onStart(int what) {
        super.onStart(what);
        callback.onStart();
      }

      @Override public void onSucceed(int what, Response<String> response) {
        super.onSucceed(what, response);
        CommonResult result = JsonUtil.jsonToObject(response.get(), CommonResult.class);
        if (result == null) return;
        if (result.getStatus() == 1) {
          callback.onSucceed(result);
        } else {
          ToastUtil.showMessage(mContext, result.getInfo());
        }
      }

      @Override public void onFailed(int what, Response<String> response) {
        super.onFailed(what, response);
        ToastUtil.showNetErrorMessage(mContext);
      }

      @Override public void onFinish(int what) {
        super.onFinish(what);
        callback.onFinish();
      }
    };
    CallServer.getInstance().request(0, request, listener);
  }

  /**
   * 提交故障报修
   */
  public static void nbnode_fault_add(final Context mContext, String adminID, String code,
      String note, String pic, final HttpRequest.HttpCallback callback) {
    StringRequest request = new StringRequest(Urls.NBNODE_FAULT_ADD, RequestMethod.POST);
    request.add("adminID", adminID);
    request.add("code", code);
    request.add("note", note);
    request.add("pic", pic);
    SimpleResponseListener<String> listener = new SimpleResponseListener<String>() {
      @Override public void onStart(int what) {
        super.onStart(what);
        callback.onStart();
      }

      @Override public void onSucceed(int what, Response<String> response) {
        super.onSucceed(what, response);
        CommonResult result = JsonUtil.jsonToObject(response.get(), CommonResult.class);

        if (result == null) return;

        if (result.getStatus() == 1) {
          callback.onSucceed(result);
        } else {
          ToastUtil.showMessage(mContext, result.getInfo());
          return;
        }
      }

      @Override public void onFailed(int what, Response<String> response) {
        super.onFailed(what, response);
        callback.onFailed();
      }

      @Override public void onFinish(int what) {
        super.onFinish(what);
        callback.onFinish();
      }
    };
    CallServer.getInstance().request(0, request, listener);
  }

  /**
   * 上传图片
   */
  public static void do_upload_head(final Context mContext, String path,
      final HttpRequest.HttpCallback callback) {
    File file = new File(path);
    StringRequest request = new StringRequest(Urls.DO_UPLOAD_PIC, RequestMethod.POST);
    request.add("filename", new FileBinary(file));
    SimpleResponseListener<String> listener = new SimpleResponseListener<String>() {
      @Override public void onStart(int what) {
        super.onStart(what);
        callback.onStart();
      }

      @Override public void onSucceed(int what, Response<String> response) {
        super.onSucceed(what, response);
        CommonResult result = JsonUtil.jsonToObject(response.get(), CommonResult.class);

        if (result == null) {
          callback.onFailed();
          return;
        }

        if (result.getStatus() == 1) {
          callback.onSucceed(result);
        } else {
          ToastUtil.showMessage(mContext, result.getInfo());
          callback.onFailed();
          return;
        }
      }

      @Override public void onFailed(int what, Response<String> response) {
        super.onFailed(what, response);
        callback.onFailed();
      }

      @Override public void onFinish(int what) {
        super.onFinish(what);
        callback.onFinish();
      }
    };
    CallServer.getInstance().request(0, request, listener);
  }

  /**
   * 停车场车位总数和空闲车位
   */
  public static void npParkNum(final Context mContext, String lotID,
      final HttpRequest.HttpCallback callback) {
    StringRequest request = new StringRequest(Urls.NPNODE_NPPARKNUM, RequestMethod.POST);
    request.add("lotID", lotID);
    SimpleResponseListener<String> listener = new SimpleResponseListener<String>() {
      @Override public void onStart(int what) {
        super.onStart(what);
        callback.onStart();
      }

      @Override public void onSucceed(int what, Response<String> response) {
        super.onSucceed(what, response);
        CommonResult result = JsonUtil.jsonToObject(response.get(), CommonResult.class);

        if (result == null) return;

        if (result.getStatus() == 1) {
          callback.onSucceed(result);
        } else {
          ToastUtil.showMessage(mContext, result.getInfo());
          return;
        }
      }

      @Override public void onFailed(int what, Response<String> response) {
        super.onFailed(what, response);
        callback.onFailed();
      }

      @Override public void onFinish(int what) {
        super.onFinish(what);
        callback.onFinish();
      }
    };
    CallServer.getInstance().request(0, request, listener);
  }
}
